<template>
    <my-update-box operation="修改密码">
        <v-form ref="form">
            <my-password-input
                    label="新密码"
                    :password.sync="updatePasswordData.password"
            ></my-password-input>
            <my-code-input
                    :email.sync="updatePasswordData.email"
                    :code.sync="updatePasswordData.code"
            ></my-code-input>
            <v-row>
                <v-col>
                    <v-btn block @click="updatePassword()" color="primary">修改</v-btn>
                </v-col>
            </v-row>
        </v-form>
    </my-update-box>
</template>

<script>
    import MyUpdateBox from "./MyUpdateBox";
    import MyPasswordInput from '../common/MyPasswordInput'
    import MyCodeInput from '../common/MyCodeInput'
    import {logout} from '../../util'

    export default {
        name: "MyUpdatePassword",
        components: { MyCodeInput, MyPasswordInput, MyUpdateBox},
        data() {
            return {
                updatePasswordData: {
                    password: '',
                    email: '',
                    code: ''
                }
            }
        },
        methods: {
            logout,
            updatePassword() {
                let self = this;
                if (!self.$refs.form.validate()) {
                    return;
                }
                self._put({
                    url: 'user/update-password',
                    data: self.updatePasswordData
                }).then(rsp => {
                    if (rsp.data.code === '00000') {
                        self.$success({message: '修改成功,请重新登录'});
                        self.logout(false);
                    } else {
                        self.$error({message: '验证码错误'})
                    }
                }).catch(error => {
                    console.log(error);
                    self.$serverError();
                })
            }
        }
    }
</script>

<style scoped>

</style>